package com.ruoyi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class TestDocker {
    public static final String JWT
            = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTU5OTIzMzIzN30.0RtFxP3NeSiYi-g8tuZlJPsTU-idg-rt5vfzSj26cTc";

    public static final String Api = "http://47.89.36.172:9000/api";

    public static void main(String[] args) {
        //https://app.swaggerhub.com/apis/deviantony/Portainer/1.23.2/#/auth/AuthenticateUser
        //https://gist.github.com/deviantony/77026d402366b4b43fa5918d41bc42f8

        //获取token
        Map<String, String> authReqBody = new HashMap<>();
        authReqBody.put("Username", "admin");
        authReqBody.put("Password", "knight2012");

        Object result = HttpClientUtils.doPost(Api+"/auth", JSON.toJSONString(authReqBody), new HashMap<>());
        log.info("create docker result is {}", JSONObject.toJSONString(result));

//
//        Object endPointsResult = HttpClientUtils.doGet(Api + "/endpoints", new HashMap<>(), new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        log.info("list endPointsResult result is {}", JSONObject.toJSONString(endPointsResult));


        Object insepctResult = HttpClientUtils.doGet(Api + "/endpoints/2/docker/containers/c14bfabc2865/json", new HashMap<>(), new HashMap<String, String>() {{
            put("Authorization", "Bearer " + JWT);
        }});
        log.info("list endPointsResult result is {}", JSONObject.toJSONString(insepctResult));

//        Object endPoints1ContainerList = HttpClientUtils.doGet(Api + "/endpoints/1/docker/containers/json", new HashMap<String, Object>() {{
//            put("all", true);
//        }}, new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        log.info("list endPoints1ContainerList result is {}", JSONObject.toJSONString(endPoints1ContainerList));

//        Object endPoints2ContainerList = HttpClientUtils.doGet(Api + "/endpoints/2/docker/containers/json", new HashMap<String, Object>() {{
//            put("all", true);
//        }}, new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        log.info("list endPoints2ContainerList result is {}", JSONObject.toJSONString(endPoints2ContainerList));
//
//
//        Map<String, String> labels = new HashMap<>();
//        labels.put("user", "neo");
//        labels.put("strategy", "kline");
//        Map<String,Boolean> bindPorts = new HashMap<>();
//        bindPorts.put("PublishAllPorts",true);

//        Object endPoints1Create = HttpClientUtils.doPost(Api + "/endpoints/1/docker/containers/create", JSON.toJSONString(new HashMap<String, Object>() {{
//            put("Image", "47.89.36.172:5000/depth:release-0.0.1");
//            put("Labels", labels);
//        }}), new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        log.info("list endPoints1ContainerList result is {}", JSONObject.toJSONString(endPoints1Create));
//
//        Object endPoints2Create = HttpClientUtils.doPost(Api + "/endpoints/2/docker/containers/create", JSON.toJSONString(new HashMap<String, Object>() {{
////            put("Image", "47.89.36.172:5000/depth:release-0.0.1");
//            put("Image", "redis:latest");
//            put("Labels", labels);
//            put("HostConfig", bindPorts);
//        }}), new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        log.info("list endPoints2ContainerList result is {}", JSONObject.toJSONString(endPoints2Create));
//
        //ID c7b948fe34cca6ee265fb8739ed5781593fe150dfd4e8872429d51ece3a9982c


//        doAction("start");
//        doAction("stop");
//        doAction("restart");
//        doAction("kill");
//        doAction("pause");
//        doAction("unpause");
//
//        Object actionDelete = HttpClientUtils.doDelete(Api + "/endpoints/2/docker/containers/c7b948fe34cca6ee265" +
//                "fb8739ed5781593fe150dfd4e8872429d51ece3a9982c?v=true&force=true", new HashMap<String, String>() {{
//            put("Authorization", "Bearer " + JWT);
//        }});
//        if (actionDelete != null)
//            log.info("delete endPoints2ContainerList result is {}", JSON.toJSONString(actionDelete));
//        else
//            log.info("delete endPoints2ContainerList");
    }

    public static void doAction(String action) {
        Object actionResult = HttpClientUtils.doPost(Api + "/endpoints/2/docker/containers/c7b948fe34cca6ee265" +
                "fb8739ed5781593fe150dfd4e8872429d51ece3a9982c/" + action, JSON.toJSONString(new HashMap<String, Object>()), new HashMap<String, String>() {{
            put("Authorization", "Bearer " + JWT);
        }});
        if (actionResult != null)
            log.info("{} container result is {}", action, JSON.toJSONString(actionResult));
        else
            log.info("{} container", action);
    }


}
